<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录香哈菜谱</view>
			<!-- #ifdef MP-WEIXIN -->
			<view class="checkBox">
				<button @tap="wxLogin" :style="[inputStyle]" class="getCaptcha">微信一键登录</button>
			</view>
			<!-- #endif -->
			<!-- #ifdef APP-VUE -->
			<view class="phoneLogin">
				<input class="u-border-bottom" type="number" v-model="tel" placeholder="请输入手机号" />
				<view class="tips">未注册的手机号验证后自动创建美团账号</view>
				<button @tap="phoneLogin" :style="[inputStyle]" class="getCaptcha">获取短信验证码</button>
				<view class="alternative">
					<view class="password">密码登录</view>
					<view class="issue">遇到问题</view>
				</view>
			</view>
			<!-- #endif -->


		</view>
		<view class="buttom">
			<view class="loginType">
				<view class="wechat item">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon">
						<u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon>
					</view>
					QQ
				</view>
			</view>
			<view class="hint">
				登录代表同意
				<text class="link">香哈菜谱用户协议、隐私政策，</text>
				并授权使用您的香哈菜谱账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tel: '',
				wxData: {
					appId: 'wx22faea785b2909a7',
					appSecret: '203c53ed4ab06802413ba324f6d63f9a'
				}
			}
		},
		computed: {
			inputStyle() {
				let style = {}
				if (this.tel) {
					style.color = '#fff'
					style.backgroundColor = this.$u.color['warning']
				}
				return style
			}
		},
		methods: {
			phoneLogin() {
				if (this.$u.test.mobile(this.tel)) {
					this.$u.route({ url: '/pages/login/code' })
				} else {
					console.log('请输入正确的手机号')
				}
			},
			wxLogin() {
				wx.getUserProfile({
					desc: '用于展示用户信息', // 必填
					success: res => {
						// wx.setStorage({
						// 	key: 'userInfo',
						// 	data: res.userInfo
						// })
						this.wxData.userInfo=res.userInfo
						// console.log(res.userInfo);
						wx.login({
							success: res => {
								this.wxData.code = res.code
								console.log(res)
								this.$api.login.wxLogin(this.wxData).then(res => {
									// console.log(res)
									wx.setStorage({
										key: 'token',
										data: res.token
									})
									this.$api.login
									.getAuth(res.token,{ token:res.token,...this.wxData.userInfo })
									.then(res=>{
										// console.log(res);
									})
									uni.switchTab({ url: '/pages/mine/mine' })
									// console.log(res.token);
								})
							}
						})
					},
					fail: err => {
						console.log('err', err)
					}
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.checkBox {
				button {
					color: white;
				}
			}

			.phoneLogin {
				button {
					color: white;
				}
			}

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				margin-bottom: 20rpx;
				background-color: rgb(239, 141, 52);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 250rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
